/*
 * enc_istener.h
 *
 *  Created on: 2018年11月6日
 *  Author: Degang.Wu (windragon@126.com)
 *  Desc:
 */

#ifndef ENC_ISTENER_H_
#define ENC_ISTENER_H_

#include <u/u_sys.h>
#include <u/u_net.h>
#include "public.h"
#include "enc_sockmsg.h"
#include <thread>

#define SUB_BOARD_STATUS_ON		1
#define SUB_BOARD_STATUS_DOWN	0

namespace hiapp
{

class Listener : public u::sys::Thread
{
public:
	Listener(U16 _port);
	virtual ~Listener();

	void onListen();
	void stop();
protected:
	virtual void run();
	virtual void onRun() = 0;
	static void* listen_thread(void* param);

	virtual int process(pU8 msg, int sock) = 0;

	virtual void onStop() = 0;

	static int WaitSignal(pthread_mutex_t* _lock, pthread_cond_t* _cond, int msec);
	static int NotifySignal(pthread_mutex_t* _lock, pthread_cond_t* _cond);

protected:
	pthread_t	th_listen;

private:
	u::net::ServerTcpSocket m_msgSrv;
	U16			nPort;

	U8	msgbuf[SZ_128K];	// buf is enough: sizeof(stENC_DEVSET_MASTER) is less than 100K
};

} /* namespace hiapp */

#endif /* ENC_ISTENER_H_ */
